﻿using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using MySql.Data.MySqlClient; // 引入MySQL的命名空间

namespace parking_program
{
    // 基类
    internal class FunctionClass
    {
    }

    // MySQL基础信息类，存储数据库连接信息
    internal class MySqlBaseInfoClass : FunctionClass
    {
        public string ServerIp { get; set; }
        public string DatabaseUsername { get; set; }
        public string DatabasePassword { get; set; }
        public string DatabaseName { get; set; }

        // 构造函数，初始化数据库连接信息
        public MySqlBaseInfoClass(string ip, string username, string password, string database)
        {
            ServerIp = ip;
            DatabaseUsername = username;
            DatabasePassword = password;
            DatabaseName = database;
        }

        // 创建数据库连接的方法
        protected MySqlConnection CreateConnection()
        {
            string connectionString = $"Server={ServerIp};Database={DatabaseName};Uid={DatabaseUsername};Pwd={DatabasePassword};";
            return new MySqlConnection(connectionString);
        }
    }

    // 从文件中读取数据库连接信息的类
    internal class MySqlConfigReaderClass : FunctionClass
    {
        public MySqlBaseInfoClass MySqlBaseInfo { get; private set; }

        // 构造函数，读取配置文件并初始化MySqlBaseInfoClass
        public MySqlConfigReaderClass(string filePath)
        {
            if (File.Exists(filePath))
            {
                string[] lines = File.ReadAllLines(filePath);
                string ip = ExtractValue(lines, "ip");
                string username = ExtractValue(lines, "username");
                string password = ExtractValue(lines, "password");
                string database = ExtractValue(lines, "database"); // 新增读取数据库名称

                MySqlBaseInfo = new MySqlBaseInfoClass(ip, username, password, database);
            }
            else
            {
                throw new FileNotFoundException("The specified file was not found.", filePath);
            }
        }

        // 提取配置文件中的值
        private string ExtractValue(string[] lines, string key)
        {
            foreach (string line in lines)
            {
                if (line.StartsWith(key + ":"))
                {
                    return line.Substring(key.Length + 1).Trim();
                }
            }
            throw new InvalidOperationException($"The key '{key}' was not found in the file.");
        }
    }

    // MySQL查询类，继承自MySqlBaseInfoClass
    internal class MySqlQueryClass : MySqlBaseInfoClass
    {
        public MySqlQueryClass(string ip, string username, string password, string database) : base(ip, username, password, database) { }

        // 执行查询并返回结果
        public DataTable QueryData(string query, params MySqlParameter[] parameters)
        {
            using (MySqlConnection conn = CreateConnection())
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand(query, conn);
                cmd.Parameters.AddRange(parameters);
                MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
                DataTable table = new DataTable();
                adapter.Fill(table);
                return table;
            }
        }
    }

    // MySQL更改类，继承自MySqlBaseInfoClass
    internal class MySqlUpdateClass : MySqlBaseInfoClass
    {
        public MySqlUpdateClass(string ip, string username, string password, string database) : base(ip, username, password, database) { }

        // 执行更新操作
        public int UpdateData(string updateQuery, params MySqlParameter[] parameters)
        {
            using (MySqlConnection conn = CreateConnection())
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand(updateQuery, conn);
                cmd.Parameters.AddRange(parameters);
                return cmd.ExecuteNonQuery();
            }
        }
    }

    // MySQL增加类，继承自MySqlBaseInfoClass
    internal class MySqlInsertClass : MySqlBaseInfoClass
    {
        public MySqlInsertClass(string ip, string username, string password, string database) : base(ip, username, password, database) { }

        // 执行插入操作并返回最后插入的ID
        public int InsertData(string insertQuery, params MySqlParameter[] parameters)
        {
            using (MySqlConnection conn = CreateConnection())
            {
                conn.Open();
                using (MySqlCommand cmd = new MySqlCommand(insertQuery, conn))
                {
                    cmd.Parameters.AddRange(parameters);
                    cmd.ExecuteNonQuery(); // 执行插入操作
                }
                // 获取最后插入的ID
                using (MySqlCommand lastIdCmd = new MySqlCommand("SELECT LAST_INSERT_ID();", conn))
                {
                    object idObj = lastIdCmd.ExecuteScalar();
                    if (idObj != null)
                    {
                        return Convert.ToInt32(idObj);
                    }
                    else
                    {
                        // 处理LAST_INSERT_ID()返回NULL的情况
                        throw new InvalidOperationException("Unable to retrieve the last inserted ID.");
                    }
                }
            }
        }
    }

    // MySQL删除类，继承自MySqlBaseInfoClass
    internal class MySqlDeleteClass : MySqlBaseInfoClass
    {
        public MySqlDeleteClass(string ip, string username, string password, string database) : base(ip, username, password, database) { }

        // 执行删除操作
        public int DeleteData(string deleteQuery, params MySqlParameter[] parameters)
        {
            using (MySqlConnection conn = CreateConnection())
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand(deleteQuery, conn);
                cmd.Parameters.AddRange(parameters);
                return cmd.ExecuteNonQuery();
            }
        }
    }

}